$(function(){
	refresh();
});

function add(){
	addSiteDiag('添加地区',function(){
    	$.post("/areaadd",{
    			name: $('#v_name').textbox('getValue')
    		}, function(data) {
    			$('#dd').dialog('close');
    			if(data=='Success'){
    				$.messager.alert('','添加完成!','');
    				refresh();
    			}else $.messager.alert('','添加失败!','error');
		});
    });
	$('#v_name').textbox('setValue','');
	$('#v_id').val('');
}

function edit(){
	var row = $('#dg').datagrid('getSelected');
    if (row){
    	addSiteDiag('修改信息',function(){
        	$.post("/areamod",{
	        		id: $('#v_id').val(),
	    			reward: $('#v_name').textbox('getValue')
        		}, function(data) {
        			$('#dd').dialog('close');
        			if(data=='Success'){
        				$.messager.alert('','修改完成!','');
        				refresh();
        			}else $.messager.alert('','未修改成功!','error');
    		});
        });
    	$('#v_name').textbox('setValue',row.name);
    	$('#v_id').val(row.id);
    }else{
		$.messager.alert('','未选中需编辑的区域!','error');
	}
}

function remove(){
	$.messager.confirm('删除地区信息', '请确认是否删除?', function(r){
		if (r){
        	var row = $('#dg').datagrid('getSelected');
            if (row){
            	$.post("/areadel",{ id: row.id }, function() {
            		$.messager.alert('', '['+row.name+"] 地区已删除。");
            		refresh();
        		});
            }
        }
    });
}

function addSiteDiag(title,handlerfunction){
	$('#dd').dialog({
	    title: title,
		closed: false,
	    cache: false,
	    modal: true,
	    buttons: [
	              {text:'保存',iconCls:'icon-ok',handler:handlerfunction},
	              {text:'取消',iconCls:'icon-cancel',handler: function(){$('#dd').dialog('close');} }
	              ]
	});
}

function search(){
	
}

function refresh(){
	loadUrlDataGrid('dg','/areaall',{},
			[[]],
			[[{field:'name',title:'地区名称',align:'center',sortable:true}
		    ]],
			'#tb');
}

function loadUrlDataGrid(div,url,queryParams,frozenColumns,columns,toolbar){
	$('#'+div).datagrid({
	    url:url,
	    queryParams:queryParams,
	    frozenColumns:frozenColumns,
	    columns:columns,
	    singleSelect:true,
	    collapsible:true,
	    rownumbers:true,
        multiSort:true,
        pagination:false,
	    toolbar: toolbar
	});
}